﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TimKen.BussLogic;
using System.Data;
using TimKen.DataEntity;

namespace TimKen.Web.Shop
{
    public partial class OrderHistory : TimKen.App.WebPage
    {
        OrderManager om = new OrderManager();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (CurrentUserName != null && CurrentUserName != "")
                {
                    BindOrderStatus(); BindData();
                }
                else
                {
                    Alert("您必须登录！", "../Index.aspx");
                }
            }
        }
        protected void BindOrderStatus()
        {
            ddlOrderStatus.Items.Add(new ListItem(GetLangString("All"), "-1"));
            ddlOrderStatus.DataTextField = "Status";
            ddlOrderStatus.DataValueField = "ID";
            ddlOrderStatus.DataSource = om.GetOrderStatus(this.LanguageID);
            ddlOrderStatus.DataBind();
        }
        public void BindData()
        {
            int totalAmountEnd = -1;
            if (txtTotalAmountEnd.Text != "")
            {
                totalAmountEnd = Int32.Parse(txtTotalAmountEnd.Text);
            }
            int totalAmountStart = -1;
            if (txtTotalAmountFrom.Text != "")
            {
                totalAmountStart = Int32.Parse(txtTotalAmountFrom.Text);
            }

            int orderStatus = int.Parse(ddlOrderStatus.SelectedValue);
            DataSet ds = om.GetList(AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex,
              txtOrderNo.Text, txtCreateDateFrom.Text, txtCreateDateTo.Text, totalAmountStart, totalAmountEnd,
               orderStatus/*该参数是合同状态此处为订单状态*/, "", txtItemCode.Text, "", txtContent.Text, CurrentUserName, (int)OrderType.Order, LanguageID);

            AspNetPager1.RecordCount = Convert.ToInt32(ds.Tables[1].Rows[0]["total"]);

            this.repOrderList.DataSource = ds;
            this.repOrderList.DataBind();
        }
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            BindData();
        }

        protected void repOrderList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "CopyOrder")
            {
                string orderid = e.CommandArgument.ToString();
                OrderItemManager oim = new OrderItemManager();
                DataSet ds = oim.GetOrderItems(orderid);
                ProductManager productManager = new ProductManager();
                UserAccountManager uam = new UserAccountManager();
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    TimKen.DataEntity.Product pr = productManager.GetModel(Convert.ToInt32(dr["ProductID"].ToString()));

                    Project pro = new Project();
                    pro.Date = Convert.ToDateTime(DateTime.Now);
                    pro.Min = Convert.ToInt32(pr.MinMultiple);
                    pro.MinQuantitative = Convert.ToInt32(pr.MinNumber);
                    pro.Netweight = float.Parse(pr.Weight.ToString());
                    pro.ProjectId = pr.ItemCode.Trim();
                    pro.ProjectXh = pr.ItemClass.Trim();
                    pro.Unitprice = Convert.ToDecimal(pr.BasicPrice);
                    pro.TaxRate = float.Parse(pr.TaxRate.ToString());
                    bool isAuthorizationed = productManager.GetProductAuthorization(Convert.ToInt32(pro.ProjectId), uam.GetModel(CurrentUserName).CustomerID.Value);
                    ShopCartItem item = new ShopCartItem(pro, Convert.ToInt32(dr["Quantity"]), isAuthorizationed);
                    this.ShopCart.Add(item);
                }
                ClientScript.RegisterStartupScript(this.GetType(), "tanyo", "<script>deleteMesg();</script>", false);
                //om.CopyOrder(orderid, CurrentUserName);
                //Response.Redirect("/Cart/View.aspx");
            }

            else if (e.CommandName == "CancelOrder")
            {
                Alert(GetLangString("CancelOrderInfo"));
                return;
            }
        }
    }
}